package com.gtotek.tumchu.activity;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import android.content.Context; 
import android.view.View;
import android.view.Window; 

import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;

import com.gtotek.tumchu.R;
import com.gtotek.tumchu.base.BaseActivity;
import com.gtotek.tumchu.base.Constans;
import com.gtotek.tumchu.customview.GifMovieView;
import com.gtotek.tumchu.dao.QuestionDao;
import com.gtotek.tumchu.util.PreferenceUtil;

public class PlayingImageGifActivity extends BaseActivity {

	private GifMovieView mImgQuestion;

	@Override
	protected void initialize() {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_playing_imagegif);
		this.mImgQuestion = (GifMovieView) this.findViewById(R.id.img_question);
		index = PreferenceUtil.getValue(mContext, Constans.KEY_INDEX_IMAGE_GIF, 0);
		super.initialize();
	}

	@Override
	protected void setType() {
		// TODO Auto-generated method stub
		type = QuestionDao.TYPE_ANH_GIF;
	}
	
	@Override
	protected void incrementIndex() {
		// TODO Auto-generated method stub
		PreferenceUtil.setValue(mContext, Constans.KEY_INDEX_IMAGE_GIF, index);
		super.incrementIndex();
		
	}

	 

	@Override
	protected void next() {
		// TODO Auto-generated method stub
		super.next();

		AjaxCallback<InputStream> ajaxCallback = new AjaxCallback<InputStream>() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				mProgressBar.setVisibility(View.VISIBLE);
				mImgQuestion.setVisibility(View.INVISIBLE);
			}
			
			

			@Override
			public void callback(String url, InputStream is, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, is, status);
				try {
					byte[] bytes = IOUtils.toByteArray(is);
					mImgQuestion.setBytes(bytes);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				mProgressBar.setVisibility(View.INVISIBLE);

				mImgQuestion.setVisibility(View.VISIBLE);
			}

		};

		mAQuery.progress(mProgressBar).ajax(this.mQuestionEntity.getImage(),
				InputStream.class, ajaxCallback);

	}

	@Override
	protected void passQuestion() {
		// TODO Auto-generated method stub
		putToActivityWinOne();
	}

 
	
	 

}
